package com.ms.game.itembox;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

import com.ms.game.itembox.container.ItemRollCore;
import com.ms.game.itembox.container.ItemRollData;
import com.ms.game.itembox.container.ItemRollGroupData;
import com.ms.game.itembox.container.ItemRollGroupList;
import com.ms.game.itembox.container.ItemRollInfo;
import com.ms.game.itembox.message.SSimpleItem;
import com.ms.game.itembox.message.SpinRollItemDataResponse;
import com.ms.player.PlayerUserType;
import com.ms.service.error.ErrorCode;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;


/**
 * 道具掉落功能 
 * @author TW15012
 *
 */
public class RollItemSys 
{
	public static String STONE = "STONE";
	
	public static String GAME_ITEM = "GAME";
	
	public static String NORMAL_ITEM = "NORMAL";
	
	//比例大於10w後的分析
	private static Integer [] bigsplit = new Integer []{
																					0,1,2,3,4,5,6,7,8,9,
																					10,11,12,13,14,15,16,17,18,19,
																					20,21,22,23,24,25,26,27,28,29,
																					30,31,32,33,34,35,36,37,38,39,
																					40,41,42,43,44,45,46,47,48,49
																				};
	
	/**
	 * 改成絕對不會骰道具
	 */
	private static final boolean rollItemEnable = false;
	
	public static ArrayList<ItemSimpleData> rollItems(String gamename,int bet,boolean isUseTrialCoin,int playerUserType)
	{
		ArrayList<ItemSimpleData> list = new ArrayList<ItemSimpleData>();

		if(!rollItemEnable){
			return list;
		}
		
		//2018-01-03 企劃說下注額用真實金額(1:1)無條件捨去到整數
		//因此0.XX會被捨去成0，所以沒下注金額就不用算了
		if(bet <= 0){
			return list;
		}
		
		// 使用試玩幣不會掉道具
		if (isUseTrialCoin) {
			return list;
		}
		
		// 小樂不會掉道具
		if (playerUserType == PlayerUserType.ROBORT) {
			return list;
		}
		
		HashMap<String,ItemRollData> data =  ItemRollInfo.getInstance().getDataMap();
		if(!data.containsKey(gamename))
		{
			return list; 
		}
		
		ItemRollCore core = data.get(gamename).getCore();
		ItemSimpleData stone = rollItem(STONE,gamename,bet,core);
		ItemSimpleData nitem = rollItem(NORMAL_ITEM,gamename,bet,core);
		ItemSimpleData gitem = rollItem(GAME_ITEM,gamename,bet,core);
		
		if(stone!=null)
		{
			list.add(stone);
		}
		
		if(gitem!=null)
		{
			list.add(gitem);
		}
		
		if(nitem!=null)
		{
			list.add(nitem);
		}
		
		return list;
	}
	
	/**
	 * 使用 debug 指令 取得item 
	 * @return
	 */
	public static ItemSimpleData getItemByDebugCmd(String itemid , int count)
	{
		ItemSimpleData item = new ItemSimpleData();
		item.setItemid(itemid);
		item.setCount(count);
		return item;
	}
	
	
	/**
	 * 推播掉落道具資訊至client
	 * @param sessionID
	 * @param items
	 */
	public static void pushRollItemResultToClient(GUID sessionID , ArrayList<SSimpleItem> items)
	{
		if(items==null || items.size()==0)
		{
			return;
		}
		
		SpinRollItemDataResponse response = new SpinRollItemDataResponse();
		response.setErrorCode(ErrorCode.SUCCESS);
		response.setItemlist(items);
		MessageAction.sendMessage(sessionID, MessageTypes.S_SPIN_ROLLITEM_RESPONSE, response);
	}
	
	
	/**
	 * 道具掉落
	 * @param bet
	 * @return
	 */
	private static ItemSimpleData rollItem(String type,String gamename,int bet,ItemRollCore core)
	{
		ItemSimpleData item = null;
		
		HashMap<String, String> map = core.getCells();
		String value = map.get(type);
		if(value == null){
			return item;
		}
		
		long rate =Long.valueOf(value);
		//long div = core.getBasicDiv();
		boolean isbingo = isBingo(gamename,core.getBasicDiv(), rate*bet);
		
		if(!isbingo)
		{
			return item;
		}
		
		//讀取道具
		ItemRollGroupData data = getItem(type , core);
		
		item = new ItemSimpleData();
		item.setItemid(data.getItemid());
		item.setCount(GameUtility.randomInt(data.getMin(), data.getMax()));
		return item;
	}
		
	/**
	 * 是否骰中物品
	 * @param itemType
	 * @param gamename
	 * @param bet
	 * @param div
	 * @param rate
	 * @return
	 */
	private static boolean isBingo(String gamename  , long div , long rate)
	{
		//這部份可以多多討論
		//ArrayList<Long> list = new ArrayList<Long>();
		//ArrayList<Long []> range = new ArrayList<Long[]>();
		
		if(rate==0)
		{
			return false;
		}
		
		
		Collections.shuffle(Arrays.asList(bigsplit));
		ArrayList<Integer> lotto = new ArrayList<Integer>(Arrays.asList(bigsplit));
		
		long rangevalue =div / lotto.size();
		int idx = (int) (GameUtility.randomInt(1, 100000000)%lotto.size());
		int startheader = lotto.get(idx);
		
		//算出中獎範圍
		Long [] value = new Long[2];
		value[0]=GameUtility.randomLong(startheader*rangevalue, (startheader*rangevalue+rangevalue)-rate) ;
		value[1]=value[0] + rate;
		
		 long bingoindex =GameUtility.randomInt(1, 1000000000) % div;
		 if(bingoindex >= value[0] && bingoindex<value[1])
		 {
			return true;
		 }
		
		return false;
	}
	
	
	/**
	 * 隨機讀取發送道具
	 * @param itemtype
	 * @param core
	 * @return
	 */
	private static ItemRollGroupData getItem(String itemtype , ItemRollCore core)
	{
		HashMap<String, ArrayList<ItemRollGroupData>> map = ItemRollGroupList
				.getInstance().getDataMap().get(core.getRollKey());
		ArrayList<ItemRollGroupData> list = map.get(itemtype);
		
		if(list.size()==1)
		{
			return list.get(0);
		}
		
		int idx =GameUtility.randomInt(1, 1000000000)%(list.size());
		return list.get(idx);
	}
}
